<?php
	//权限类
	class privilege
	{
		var $database = null;

		function privilege()
		{
			$this->database = load( 'database' );
		}

		//按权限类型划分，返回权限数据的数组
		function dict()
		{
			$data = array();
			$temp = $this->database->select( 'select privilege.id, privilege.name, privilege.type_id, privilege_type.name as type_name from privilege, privilege_type where privilege.type_id = privilege_type.id' );

			foreach( $temp as $value )
			{
				if ( ! isset( $data[$value['type_id']] ) )
				{
					$data[$value['type_id']] = array();
					$data[$value['type_id']]['data'] = array();
					$data[$value['type_id']]['name'] = $value['type_name'];
				}

				$data[$value['type_id']]['data'][] = $value;
			}

			return $data;
		}

		//返回权限列表数据
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 12;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$type_id = isset( $data['type_id'] ) && preg_match( '!^\d+$!', $data['type_id'] ) ? $data['type_id'] : '';
			$order = isset( $data['order'] ) ? $data['order'] : 'privilege.id asc';
			$count = 'from privilege, privilege_type where privilege.type_id = privilege_type.id';
			if ( $type_id != '' ) $count .= ' and privilege_type.id = ' . $type_id;
			if ( isset( $data['name'] ) && $data['name'] != '' ) $count .= " and privilege.name like '%" . $data['name'] . "%'";
			$list = 'select privilege.*, privilege_type.name as type_name ' . $count . ' order by ' . $order;
			$result = $this->database->split( $count, $list, $size, $page );
			return $result;
		}

		//获取某权限的信息
		function get( $data )
		{
			$result = array();
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) ) $result = $this->database->unique( 'select * from privilege where id = ' . $data['id'] );
			return $result;
		}

		//修改某权限
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['id']['kind'] = '';
			$field['id']['type'] = 'num';
			$field['id']['min'] = '1';
			$field['id']['max'] = '8';
			$field['id']['info'] = '请传递ID';

			$field['name']['kind'] = '';
			$field['name']['type'] = 'text';
			$field['name']['min'] = '1';
			$field['name']['max'] = '32';
			$field['name']['info'] = '请输入名称';

			$field['type_id']['kind'] = '';
			$field['type_id']['type'] = 'num';
			$field['type_id']['min'] = '1';
			$field['type_id']['max'] = '8';
			$field['type_id']['info'] = '请选择类型';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$this->database->start();
				$this->database->set( 'privilege', $data, array( 'id' => $data['id'] ) );

				if ( $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $data['id'];
				}
				else
				{
					$result['message'][] = '执行修改失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//创建某权限
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['name']['kind'] = '';
			$field['name']['type'] = 'text';
			$field['name']['min'] = '1';
			$field['name']['max'] = '32';
			$field['name']['info'] = '请输入名称';

			$field['type_id']['kind'] = '';
			$field['type_id']['type'] = 'num';
			$field['type_id']['min'] = '1';
			$field['type_id']['max'] = '8';
			$field['type_id']['info'] = '请选择类型';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$this->database->start();
				$id = $this->database->add( 'privilege', $data );

				if ( preg_match( '!^\d+$!', $id ) && $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $id;
				}
				else
				{
					$result['message'][] = '执行创建失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}
	}
?>